﻿using Candy.Web;
using Candy.Web.Data;
using System;
using System.ServiceModel.DomainServices.Client;
using System.ServiceModel.DomainServices.Client.ApplicationServices;

namespace Candy.Model
{

    public class UserManager : IUserManager
    {

        private AuthUser _user;
        public AuthUser AuthenticatedUser
        {
            get
            {
                return _user;
            }
            private set
            {
                _user = value;

            }
        }


        Action<AuthUser> _loginAction;
        /// <summary>
        /// Executes the login process with a given user name and password/password hash.
        /// </summary>
        /// <param name="user">The user name.</param>
        /// <param name="passwordHash">The password or better a password hash for the login.</param>
        public void LoginAsync(LoginParameters credentials, Action<LoginOperation> loginAction)
        {
            //_loginAction = loginAction; //remember callback
            //   WebContext.Current.Authentication.LoggedIn += new EventHandler<AuthenticationEventArgs>(Authentication_LoggedIn); //register for login event
            OperationBase ret = WebContext.Current.Authentication.Login(credentials,
                (loginOperation) =>
                {
                    if (loginOperation.LoginSuccess)
                        AuthenticatedUser = (AuthUser)loginOperation.User;
                    if (loginAction != null)
                        loginAction(loginOperation);

                    if (loginOperation.LoginSuccess && LoggedIn != null)
                        LoggedIn(this, EventArgs.Empty);

                  //  AuthenticatedUser.AllowedProjectCount = 333;
                    //AuthenticatedUser.PictureURL = "http://galiano.biol.uvic.ca/ProfileFiles/default.jpg";
                    //AuthenticatedUser.FriendlyName = "None";
                    //WebContext.Current.Authentication.SaveUser((op) =>
                    //    {

                    //    }, null);

                }, null
                ); //execute login

        }

        //Login completed
        //private void Authentication_LoggedIn(object sender, AuthenticationEventArgs ea)
        //{
        //    WebContext.Current.Authentication.LoggedIn -= new EventHandler<System.ServiceModel.DomainServices.Client.ApplicationServices.AuthenticationEventArgs>(Authentication_LoggedIn);

        //    if (_loginAction != null)
        //        _loginAction((AuthUser)ea.User);
        //}


        /// <summary>
        /// Performs the logout for the current user.
        /// </summary>
        /// <param name="logoutAction">A callback to be executed when logout is completed</param>
        public void LogoutAsync(Action<LogoutOperation> logoutAction)
        {
            WebContext.Current.Authentication.Logout((lo) =>
            {
                if (logoutAction != null)
                    logoutAction(lo);
                if (lo.IsComplete && LoggedOut != null)
                    LoggedOut(this, EventArgs.Empty);
            }
            , null);
        }


        // Action<CreateUserStatus> _registerAction;

        /// <summary>
        /// Registers a new user in the user database
        /// </summary>
        /// <param name="user">The name for the user.</param>
        /// <param name="password">The password for the user.</param>
        /// <param name="email">A valid e-mail address</param>
        /// <param name="registerAction">A callback to be executed when the register process is completed.</param>
        public void RegisterAsync(RegistrationData regData, string password, Action<CandyInvokeOperation<CreateUserStatus>> registerAction)
        {
            UserRegistrationContext register = new UserRegistrationContext();

            register.CreateUser(regData, password,
                (io) =>
                {
                    if (registerAction != null)
                        registerAction(new CandyInvokeOperation<CreateUserStatus>(io));

                }

                , null);

        }


        public event EventHandler LoggedOut;

        public event EventHandler LoggedIn;
    }
}
